package org.example;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ConsumerDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory(); //创建连接工厂
        factory.setHost("123.57.1.114"); //设置IP
        factory.setPort(5672); //前面说了，客户端与RabbitMQ Server连接使用的端口默认是5672
        factory.setVirtualHost("test"); //Virtual Host
        factory.setUsername("admin"); //用户名
        factory.setPassword("admin"); //密码
        //建立连接Connection
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("hello",true, false, false, null);
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收到消息" + new String(body));
            }
        };
        channel.basicConsume("hello",false,consumer);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
//        channel.close();
//        connection.close();
    }
}
